<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends TXT_Controller{

    /**
     * _before 
     * 执行方法前执行
     * 
     * @access protected
     * @return boolean
     */
    protected function _before($method) {

        // 忽略检查登录的方法
        $ignores = array('login', 'cap');

        if (!in_array($method, $ignores)) {
            $this->_checkLogin();
        }

        return true;
    }

    /**
     * index 
     * 主页索引页(首页)
     * 
     * @access public
     * @return void
     */
    public function index() {

        // 转向短信页
        redirect('/sms');
    }

    /**
     * login 
     * 登录页
     * 
     * @access public
     * @return void
     */
    public function login() {

        // 如果已经登录，转向首页
        if ($this->_isLogin()) {
            redirect('/main/index');
        }

        // 如果点击登录提交
        if ($this->input->is_post()) {

            // 表单参数
            $uName = $this->input->post('uName');
            $uPasswd = $this->input->post('uPasswd');
            $captcha = $this->input->post('captcha');

            $errors = array();

            // 验证码失败
            if (!$this->_verifyCaptcha($captcha)) {
                $cap = $this->_generateCaptcha();
                return $this->_ajaxMessage(1, '验证码错误', $cap['image_url']);
            }

            // 验证用户名与密码
            $this->load->model('user_model');
            if (!($uid = $this->user_model->authUser($uName, $uPasswd))) {
                return $this->_ajaxMessage(2, '用户名或者密码错误');
            } else {
                $this->session->set_userdata('l', true);
                $this->session->set_userdata('uid',$uid);
                return $this->_ajaxMessage(0);
            }
        }

        // 显示登录页面
        $data = array(
            'title' => '登录',
        );

        $cap = $this->_generateCaptcha();
        $data['image'] = $cap['image'];

        $this->load->view('main/login', $data);
    }

    /**
     * cap 
     * 验证码
     * 
     * @access public
     * @return void
     */
    public function cap() {

        $cap = $this->_generateCaptcha();
        $this->load->helper('url');
        redirect($cap['image_url'], 'location', 301);
    }

    /**
     * logout 
     * 退出登录
     * 
     * @access public
     * @return void
     */
    public function logout() {

        $this->session->sess_destroy();
        redirect('/login');
    }

    /**
     * _generateCaptcha 
     * 生成验证码
     * 
     * @access private
     * @return void
     */
    private function _generateCaptcha() {

        $this->load->helper('captcha');

        $vals = array(
            'word' => '',
            'word_length' => 4,
            'img_path' => './captcha/',
            'img_url' => '/captcha/',
            'font_path' => './misc/fonts/stroke.ttf',
            'img_width' => 86,
            'img_height' => 30,
            'expiration' => 600
        );

        $cap = create_captcha($vals);
        $this->session->set_userdata('captcha', $cap['word']);

        return $cap;
    }

    /**
     * _verifyCaptcha 
     * 校验验证码
     * 
     * @param mixed $captcha 
     * @access private
     * @return void
     */
    private function _verifyCaptcha($captcha) {

        $captcha = strtolower($captcha);
        $sessCaptcha = strtolower($this->session->userdata('captcha'));
        $result = false;

        if ($captcha == $sessCaptcha) {
            $result = true;
            $this->session->unset_userdata('captcha');
        }

        return $result;
    }
}

